// 消息服务模块
import { ElMessage, ElMessageBox, ElNotification } from 'element-plus'

// 默认配置
const defaultConfig = {
  duration: 3000,
  showClose: true,
  customClass: 'custom-message',
  offset: 20
}

// 消息ID计数器
let messageId = 0

// 消息实例映射
const messageInstances = new Map()

// 基础消息方法
function baseMessage(options, type) {
  // 如果options是字符串，则转换为对象
  if (typeof options === 'string') {
    options = { message: options }
  }

  // 合并默认配置和用户配置
  const config = {
    ...defaultConfig,
    ...options,
    type,
    id: `message-${++messageId}`
  }

  // 创建消息实例
  const messageInstance = ElMessage(config)
  
  // 存储消息实例
  messageInstances.set(config.id, messageInstance)
  
  // 消息关闭后移除实例引用
  const originalCallback = config.onClose
  config.onClose = () => {
    messageInstances.delete(config.id)
    if (typeof originalCallback === 'function') {
      originalCallback()
    }
  }

  return messageInstance
}

// 成功消息
export function success(options) {
  return baseMessage(options, 'success')
}

// 警告消息
export function warning(options) {
  return baseMessage(options, 'warning')
}

// 错误消息
export function error(options) {
  return baseMessage(options, 'error')
}

// 信息消息
export function info(options) {
  return baseMessage(options, 'info')
}

// 关闭指定消息
export function close(messageId) {
  if (messageInstances.has(messageId)) {
    const instance = messageInstances.get(messageId)
    instance.close()
    messageInstances.delete(messageId)
  }
}

// 关闭所有消息
export function closeAll() {
  ElMessage.closeAll()
  messageInstances.clear()
}

// 确认对话框
export function confirm(options) {
  // 如果options是字符串，则转换为对象
  if (typeof options === 'string') {
    options = { message: options }
  }

  // 合并默认配置和用户配置
  const config = {
    title: '提示',
    message: '',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    ...options
  }

  return ElMessageBox.confirm(
    config.message,
    config.title,
    {
      confirmButtonText: config.confirmButtonText,
      cancelButtonText: config.cancelButtonText,
      type: config.type,
      customClass: config.customClass,
      beforeClose: config.beforeClose,
      showCancelButton: config.showCancelButton !== false,
      showConfirmButton: config.showConfirmButton !== false,
      distinguishCancelAndClose: config.distinguishCancelAndClose,
      dangerouslyUseHTMLString: config.dangerouslyUseHTMLString,
      center: config.center,
      roundButton: config.roundButton,
      buttonSize: config.buttonSize
    }
  )
}

// 提示对话框
export function alert(options) {
  // 如果options是字符串，则转换为对象
  if (typeof options === 'string') {
    options = { message: options }
  }

  // 合并默认配置和用户配置
  const config = {
    title: '提示',
    message: '',
    confirmButtonText: '确定',
    type: 'info',
    ...options
  }

  return ElMessageBox.alert(
    config.message,
    config.title,
    {
      confirmButtonText: config.confirmButtonText,
      type: config.type,
      customClass: config.customClass,
      beforeClose: config.beforeClose,
      showConfirmButton: config.showConfirmButton !== false,
      dangerouslyUseHTMLString: config.dangerouslyUseHTMLString,
      center: config.center,
      roundButton: config.roundButton,
      buttonSize: config.buttonSize
    }
  )
}

// 输入对话框
export function prompt(options) {
  // 如果options是字符串，则转换为对象
  if (typeof options === 'string') {
    options = { message: options }
  }

  // 合并默认配置和用户配置
  const config = {
    title: '提示',
    message: '',
    inputPlaceholder: '',
    inputType: 'text',
    inputValue: '',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    ...options
  }

  return ElMessageBox.prompt(
    config.message,
    config.title,
    {
      confirmButtonText: config.confirmButtonText,
      cancelButtonText: config.cancelButtonText,
      inputPlaceholder: config.inputPlaceholder,
      inputType: config.inputType,
      inputValue: config.inputValue,
      inputPattern: config.inputPattern,
      inputValidator: config.inputValidator,
      inputErrorMessage: config.inputErrorMessage,
      showCancelButton: config.showCancelButton !== false,
      showConfirmButton: config.showConfirmButton !== false,
      customClass: config.customClass,
      beforeClose: config.beforeClose,
      dangerouslyUseHTMLString: config.dangerouslyUseHTMLString,
      center: config.center,
      roundButton: config.roundButton,
      buttonSize: config.buttonSize
    }
  )
}

// 通知组件
export function notify(options) {
  // 如果options是字符串，则转换为对象
  if (typeof options === 'string') {
    options = { message: options }
  }

  // 合并默认配置和用户配置
  const config = {
    title: '通知',
    message: '',
    duration: 4500,
    position: 'top-right',
    showClose: true,
    customClass: 'custom-notification',
    ...options
  }

  return ElNotification(config)
}

// 成功通知
export function successNotify(options) {
  return notify({
    ...(typeof options === 'string' ? { message: options } : options),
    type: 'success'
  })
}

// 警告通知
export function warningNotify(options) {
  return notify({
    ...(typeof options === 'string' ? { message: options } : options),
    type: 'warning'
  })
}

// 错误通知
export function errorNotify(options) {
  return notify({
    ...(typeof options === 'string' ? { message: options } : options),
    type: 'error'
  })
}

// 信息通知
export function infoNotify(options) {
  return notify({
    ...(typeof options === 'string' ? { message: options } : options),
    type: 'info'
  })
}

// 关闭指定通知
export function closeNotify(instance) {
  if (instance && instance.close) {
    instance.close()
  }
}

// 关闭所有通知
export function closeAllNotifies() {
  ElNotification.closeAll()
}

// 导出主服务对象
export default {
  // 消息方法
  success,
  warning,
  error,
  info,
  close,
  closeAll,
  
  // 对话框方法
  confirm,
  alert,
  prompt,
  
  // 通知方法
  notify,
  successNotify,
  warningNotify,
  errorNotify,
  infoNotify,
  closeNotify,
  closeAllNotifies,
  
  // 原始组件实例
  ElMessage,
  ElMessageBox,
  ElNotification
}

// 在应用启动时可以调用此函数将消息服务挂载到全局
// export function setupMessageService(app) {
//   app.config.globalProperties.$messageService = messageService
//   app.provide('messageService', messageService)
// }